/*============================================================================*/
/*       UNIVERSIDAD DE GUADALAJARA - CONTINENTAL AUTOMOTIVE SYSTEMS          */
/*============================================================================*/
/*                        OBJECT SPECIFICATION                                */
/*============================================================================*
* %name:            sw_adcs.c%
* %version:         1.0%
* %created_by:      % team 3
* %date_created:    %
*=============================================================================*/
/* DESCRIPTION : ADCS functions                                               */
/*============================================================================*/
/* FUNCTION COMMENT : Provide APIs to use adcs functions 					  */
/*                                                                            */
/*                                                                            */
/*============================================================================*/


/* Includes */
/* -------- */
#include "stdtypedef.h"
#include "p18f4550.h"
#include "hw_adcs.h"

#ifndef hw_adcs_c
	#define hw_adcs_c

	// DEFINING LIGHT MEASURES LEVEL
	
	#define HalfLightMeasure 0x46 // LIGHT MEASUREMENT IS BETWEEN 40% AND 79% => SYSTEM STATE HAVE TO GO TO HalfLights 
	#define LowLightMeasure  0X69 // LIGHT MEASUREMENT: < 40% => SYSTEM STATE HAVE TO GO TO LightsOn 
	
	void initadc(void)
	{
	
		ADCON0bits.ADON = 1;
		ADCON0bits.CHS0 = 0;
		ADCON0bits.CHS1 = 0;
		ADCON0bits.CHS2 = 0;
		ADCON0bits.CHS3 = 0;
	
		ADCON1bits.VCFG0 = 0;
		ADCON1bits.VCFG1 = 0;
		ADCON1bits.PCFG0 = 0;
		ADCON1bits.PCFG1 = 1;
		ADCON1bits.PCFG2 = 1;
		ADCON1bits.PCFG3 = 1;
	
		ADCON2bits.ADFM  = 0;
		ADCON2bits.ADCS0 = 1;
		ADCON2bits.ADCS1 = 0;
		ADCON2bits.ADCS2 = 0;
	
	}
	
	T_UBYTE LightMeasure(void)
	{
		T_UBYTE lub_Measure;
		// STARTING CONVERTION
		ADCON0bits.GO = 1;
		while(ADCON0bits.GO)

		{
			;
		}
		// SAVING COMPLETE ADC CONVERTION VALUE IN luw_Measure
		lub_Measure = ADRESH;
		
		// DETERMINETING LIGHT STATUS FOR nnDECIDE RETURNING STATE
		if (lub_Measure < HalfLightMeasure)
		{
			return LightsOff;
		}
		else if (lub_Measure < LowLightMeasure)
		{
			return HalfLights;
		}
		else
		{
			return LightsOn;
		}
	}

#endif